#include "../../../libraries/sockets/udp_socket.h"
#include "../../../libraries/operations/operations.h"
#include <iostream>
#include <sstream>
#include <string>

#include <cstdlib>


using namespace std; 

int main(int argc, char *argv[])

{
    if (argc<2)
    {
        cout <<"use this format: ./server IP portno" <<endl;
        exit(1);
    }

    UDP_socket serverUDP; //Creamos socket

    serverUDP.setPortno(atoi(argv[1])); //Asignamos puerto

    serverUDP.bind(); //Enlazamos

    enum oper{Sm, Rs, Dv, Mp, Ex, Rz };
    string op, summ1, summ2, fin;

    do
    {
        int Sum1=0, Sum2=0, result=0;

        serverUDP >> op;

        serverUDP.setReceiverAdress();

        serverUDP << "Enter one number: \n";
        serverUDP >> summ1;

        Sum1=atoi(summ1.c_str());


        if(atoi(op.c_str()) != Rz)
        {
            serverUDP<< "Enter another number: \n";
            serverUDP >> summ2;
            Sum2=atoi(summ2.c_str());
        }



        switch (atoi(op.c_str()))
        {
        case Sm:
            result=suma(Sum1,Sum2);
            break;

        case Rs:
            result=resta(Sum1,Sum2);
            break;

        case Dv:
            result=divi(Sum1,Sum2);
            break;

        case Mp:
            result=mult(Sum1,Sum2);
            break;

        case Ex:
            result=exp(Sum1,Sum2);
            break;

        case Rz:
            result=raiz(Sum1);
            break;

        case -1:
            cout << "Operación no reconocida\n" << endl;
            break;
        }//end switch


        string result_str; //Convert int to string

        stringstream result_ss;
        result_ss << result;
        result_str = result_ss.str();

        serverUDP << result_str;

        serverUDP >> fin;

    }while(1); //El servidor se quedará esperando a que lleguen más clientes

    return 0;
}
